package com.gyy.flutter.plugin.flutter_plugin_print_a300.newprint;

import static com.gyy.flutter.plugin.flutter_plugin_print_a300.common.BitmapUtils.zoomImg;

import android.content.res.Resources;
import android.graphics.Bitmap;

import com.gyy.flutter.plugin.flutter_plugin_print_a300.common.StringUtil;
import com.gyy.flutter.plugin.flutter_plugin_print_a300.newprint.base.PcPointPrint;

import HPRTAndroidSDKA300.HPRTPrinterHelper;


/**
 * Date: 2021/8/16 10:13
 * Description: 汉印 A300
 * 单位 dot（8dot = 1mm）
 */
public class HMA300Print extends PcPointPrint {

    int scale = 8;
    double pageHeight;
    int setPageWidth;
    @Override
    public void pageStart(double pageWidth, double pageHeight) {
        this.pageHeight = pageHeight * scale;
        this.setPageWidth = (int) pageWidth * scale;
        if (pageWidth > 72) {
            setPageWidth = 72 * scale;
        }

        try {
            //水平偏移量；水平方向的 dpi；垂直方向的 dpi；整个标签的高度；打印的次数
            HPRTPrinterHelper.printAreaSize("0", setPageWidth + "", this.pageHeight  + "", this.pageHeight  + "", "1");
            HPRTPrinterHelper.Speed("5");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void printLine(double startX, double startY, double endX, double endY, int lineWidth) {
        double start = startX * scale;
        //线的起点大于 纸张宽度的，不打印
        if (start >= setPageWidth) {
            return;
        }
        try {
            HPRTPrinterHelper.Line(start + "", startY * scale + "", endX * scale + "", endY * scale + "", lineWidth + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param startX
     * @param startY
     * @param width
     * @param height
     * @param text
     * @param textSize
     * @param angle
     * @param isBold
     * @param reverse
     * @param mag 放大倍数
     */
    @Override
    public void printText(int startX, int startY, double width, double height, String text, double textSize, int angle, boolean isBold, boolean reverse, String mag) {
        try {

            if (!StringUtil.isEmpty(text)) {
                String command = "";
                if (angle == 0) {
                    command = HPRTPrinterHelper.TEXT;
                } else {
                    command = HPRTPrinterHelper.TEXT270;
                }
//                String textSizeStr = textSize + "";

                if (isBold) {
                    //对下面的字体进行放大
                    HPRTPrinterHelper.SetMag(mag, mag);
                }
                //文字的方向；字体点阵大小；字体大小（该功能被屏蔽统一参数传 0）；起始坐标x,y；文本数据
//                HPRTPrinterHelper.Text(command, textSizeStr, "0", startX * scale + "", startY * scale + "", text);
                HPRTPrinterHelper.AutLine(startX * scale + "", startY * scale + "", (int)width * 4, (int)textSize, false, false, text);
                if (isBold) {
                    //关闭放大
                    HPRTPrinterHelper.SetMag("1", "1");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void printBarCode(double startX, double startY, String text, double Height, int LineWidth, int Rotate) {
        double barHeight = scale * Height;
        if (barHeight > 70) {
            //大于70的条码，打印有问题
            barHeight = 70;
        }
        try {
//            if (Rotate == 90) {
//                HPRTPrinterHelper.Barcode(HPRTPrinterHelper.VBARCODE, HPRTPrinterHelper.code128,
//                        //窄条的单位宽度；宽条窄条的比例；条码高度
//                        (LineWidth - 1) + "", "2", StringUtil.getIntValue(barHeight) + "",
//                        //起始x，y坐标
//                        startX * scale + "", startY * scale + "",
//                        //条码下方的数据是否可见；字体的类型
//                        false, "7",
//                        //字体的大小；条码与文字间的距离 ；条码数据
//                        "1", "5", text);
//            } else if (Rotate == 270) {
//                HPRTPrinterHelper.Barcode(HPRTPrinterHelper.VBARCODE, HPRTPrinterHelper.code128,
//                        //窄条的单位宽度；宽条窄条的比例；条码高度
//                        (LineWidth - 1) + "", "2", StringUtil.getIntValue(barHeight) + "",
//                        //起始x，y坐标
//                        (startX - Height) * scale + "", (startY + 18 * LineWidth) * scale + "",
//                        //条码下方的数据是否可见；字体的类型
//                        false, "7",
//                        //字体的大小；条码与文字间的距离 ；条码数据
//                        "1", "5", text);
//            } else {
//                HPRTPrinterHelper.Barcode(HPRTPrinterHelper.BARCODE, HPRTPrinterHelper.code128,
//                        //窄条的单位宽度；宽条窄条的比例；条码高度
//                        (LineWidth - 1) + "", "2", StringUtil.getIntValue(barHeight) + "",
//                        //起始x，y坐标
//                        startX * scale + "", startY * scale + "",
//                        //条码下方的数据是否可见；字体的类型
//                        false, "7",
//                        //字体的大小；条码与文字间的距离 ；条码数据
//                        "1", "5", text);
//            }
            if (Rotate == 270) {
                HPRTPrinterHelper.Barcode(HPRTPrinterHelper.VBARCODE, HPRTPrinterHelper.code128,
                        //窄条的单位宽度；宽条窄条的比例；条码高度
                        (LineWidth - 1) + "", "2", StringUtil.getIntValue(barHeight) + "",
                        //起始x，y坐标
                        (21) * scale + "", (49) * scale + "",
                        //条码下方的数据是否可见；字体的类型
                        false, "7",
                        //字体的大小；条码与文字间的距离 ；条码数据
                        "1", "5", text);
            } else {
                HPRTPrinterHelper.Barcode(HPRTPrinterHelper.BARCODE, HPRTPrinterHelper.code128,
                        //窄条的单位宽度；宽条窄条的比例；条码高度
                        (LineWidth - 1) + "", "2", StringUtil.getIntValue(barHeight) + "",
                        //起始x，y坐标
                        startX * scale + "", startY * scale + "",
                        //条码下方的数据是否可见；字体的类型
                        false, "7",
                        //字体的大小；条码与文字间的距离 ；条码数据
                        "1", "5", text);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void printQRCode(String x, String y, String M, String U, String data) {
        try {
            HPRTPrinterHelper.PrintQR(HPRTPrinterHelper.BARCODE, x, y, M, U, data);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void pageFree() {
        try {
            HPRTPrinterHelper.Form();
            HPRTPrinterHelper.Print();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void printPcDraw(Resources res, Bitmap bitmap, int width, int height, double startX, double StartY, boolean isNegate) {
        Bitmap mBitmap = zoomImg(bitmap, width * scale, height * scale);
        try {
            HPRTPrinterHelper.Expanded(startX * scale + "", StartY * scale + "", mBitmap,
                    //图片算法。0：二值算法；1：半色调算法
                    0);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void getPrintState() {
    }

}
